package com.hualan.servlet;

import com.alibaba.fastjson.JSON;

import com.hualan.bean.PanghuCommunity;
import com.hualan.service.CommunityUpdateService;
import com.hualan.service.impl.CommunityUpdateServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/community/communityUpdate")
public class CommunityUpdateServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //communityName, communityProvince, communityCity, communityDistrict,communityLatitude, communityLongitude, communityAddress, communityId,
        Map<String,Object> map = new HashMap<>();
        String communityId = req.getParameter("communityId");
        int cid = Integer.parseInt(communityId);
        String communityName = req.getParameter("communityName");
        String communityProvince = req.getParameter("communityProvince");
        String communityCity = req.getParameter("communityCity");
        String communityDistrict = req.getParameter("communityDistrict");
        String communityLatitude = req.getParameter("communityLatitude");
        String communityLongitude = req.getParameter("communityLongitude");
        String communityAddress = req.getParameter("communityAddress");
        communityAddress = communityProvince + communityCity + communityDistrict + communityAddress;

        PanghuCommunity panghuCommunity = new PanghuCommunity(cid,communityName,communityAddress,communityProvince,communityCity,communityDistrict,communityLatitude,communityLongitude);
        CommunityUpdateService communityUpdateService = new CommunityUpdateServiceImpl();
        int row = communityUpdateService.updateCommunity(panghuCommunity);
        if (row < 0){
            map.put("code",302);
            map.put("message","更新失败");
        }else{
            map.put("code",200);
            map.put("message","更新成功");
        }
        resp.getWriter().write(JSON.toJSONString(map));
    }
}
